﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.IO.IsolatedStorage;

namespace Wordpress.Phone.Models
{
    public class MovieData
    {
        private static MovieData _instance;

        protected MovieCache _cache;

        protected IsolatedStorageSettings _settings;

        public event EventHandler<EventArgs> LoadMoviesCompleted;
        public event EventHandler<EventArgs> SaveMoviesCompleted;
        public event EventHandler<EventArgs> Synchronized;

        private bool _isLoaded = false;

        public bool IsLoaded
        {
            get
            {
                return this._isLoaded;
            }
        }

        protected List<Movie> _movies = null;

        public List<Movie> Movies
        {
            get
            {
                return _movies;
            }
            private set
            {
                _movies = value;
            }
        }

        private DateTime _lastSynchro;


        public DateTime LastSynchro
        {
            get
            {
                if (this._settings.Contains("lastSynchro")) {
                    this._lastSynchro = (DateTime)this._settings["lastSynchro"];
                }
                return this._lastSynchro;
            }
            private set
            {
                this._lastSynchro = value;
                if (this._settings.Contains("lastSynchro"))
                {
                    this._settings["lastSynchro"] = value;
                }
                else
                {
                    this._settings.Add("lastSynchro", value);
                    this._settings.Save();
                }
            }
        }

        protected ISource _source;

        public ISource Source
        {
            get
            {
                if (this._source == null)
                {
                    this.Source = new MRssSource();
                }
                return this._source;
            }
            set
            {
                if (value != this._source) {
                    this._source = value;
                    this._source.LoadCompleted += new EventHandler<EventArgs>(source_LoadCompleted);
                    this._source.SaveCompleted += new EventHandler<EventArgs>(source_SaveCompleted);
                    }
            }
        }

        public MovieData()
        {
            this._settings = IsolatedStorageSettings.ApplicationSettings;
            
            this._cache = new MovieCache();
            this._cache.LoadCompleted += new EventHandler<EventArgs>(_cache_LoadCompleted);
            this._cache.SaveCompleted += new EventHandler<EventArgs>(_cache_SaveCompleted);

            this._movies = new List<Movie>();
        }

        public void LoadMovies()
        {
            this.LoadMovies(false);
        }

        public void LoadMovies(bool force)
        {
            if (this._isLoaded == true && force == false)
            {
                return;
            }

            if (this._isLoaded == true && force == true) {
                this._movies.Clear();
                this.Source.Load();
            }
            else
            {
                this._movies.Clear();
                DateTime now = DateTime.Now;
                if (now.Subtract(this.LastSynchro) > TimeSpan.FromDays(7) || force == true)
                {
                    this.Source.Load();
                }
                else
                {
                    this._cache.Load();
                }
            }
        }

        public void SaveMovies()
        {
            this.Source.Save(this._movies);
        }

        void source_LoadCompleted(object sender, EventArgs e)
        {
            ISource source = sender as ISource;
            IEnumerable<Movie> loadedMovie = source.Items();

            foreach (Movie m in loadedMovie) {
                this.Movies.Add(m);
            }

            this._isLoaded = true;

            if (this.LoadMoviesCompleted != null)
            {
                this.LoadMoviesCompleted(this, new EventArgs());
            }

            this._cache.Save(this._movies);
        }

        void source_SaveCompleted(object sender, EventArgs e)
        {
            if (this.SaveMoviesCompleted != null)
            {
                this.SaveMoviesCompleted(this, new EventArgs());
            }
        }

        void _cache_LoadCompleted(object sender, EventArgs e)
        {
            IEnumerable<Movie> loadedMovie = this._cache.Items();

            foreach (Movie m in loadedMovie)
            {
                this.Movies.Add(m);
            }

            this._isLoaded = true;

            if (this.LoadMoviesCompleted != null)
            {
                this.LoadMoviesCompleted(this, new EventArgs());
            }

            if (this._settings.Contains("lastSynchro"))
            {
                this.LastSynchro = (DateTime)this._settings["lastSynchro"];
                this.Synchronized(this, new EventArgs());
            }
        }

        void _cache_SaveCompleted(object sender, EventArgs e)
        {
            this.LastSynchro = DateTime.Now;
            this.Synchronized(this, new EventArgs());
        }

        public static MovieData GetInstance()
        {
            if (MovieData._instance == null)
            {
                MovieData._instance = new MovieData();
            }
            return MovieData._instance;
        }
    }
}
